# -*- coding:utf-8 -*-

import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json
from datetime import datetime

requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告

"""
天眼查
"""
class TianYanCha():
    def __init__(self):
        #声明一个CookieJar对象实例来保存cookie
        self.cookie = cookiejar.CookieJar()
        ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
        self.headers = {
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
            "Accept-Encoding": "gzip, deflate, br",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Cache-Control": "max-age=0",
            "Connection": "keep-alive",
            "Cookie": "aliyungf_tc=AQAAAP9Qxg3J4wwAwxZydyCb7wKFLqE0; ssuid=2948597306; bannerFlag=undefined; csrfToken=ODUDSHAkax3eo2wWExO6D8Rq; TYCID=244396701e6911ea93782b205bbc7c82; undefined=244396701e6911ea93782b205bbc7c82; Hm_lvt_e92c8d65d92d534b0fc290df538b4758=1576324752; _ga=GA1.2.1107016836.1576324752; _gid=GA1.2.2104052934.1576324752; token=6590b0b30223496ab805f2db0584d53c; _utm=582bcec479b744819c9db44f7b5ce0b7; tyc-user-info=%257B%2522claimEditPoint%2522%253A%25220%2522%252C%2522myAnswerCount%2522%253A%25220%2522%252C%2522myQuestionCount%2522%253A%25220%2522%252C%2522signUp%2522%253A%25220%2522%252C%2522explainPoint%2522%253A%25220%2522%252C%2522privateMessagePointWeb%2522%253A%25220%2522%252C%2522nickname%2522%253A%2522%25E7%258E%2589%25E6%25BC%25B1%2522%252C%2522integrity%2522%253A%25220%2525%2522%252C%2522privateMessagePoint%2522%253A%25220%2522%252C%2522state%2522%253A%25220%2522%252C%2522announcementPoint%2522%253A%25220%2522%252C%2522isClaim%2522%253A%25220%2522%252C%2522bidSubscribe%2522%253A%2522-1%2522%252C%2522vipManager%2522%253A%25220%2522%252C%2522discussCommendCount%2522%253A%25220%2522%252C%2522monitorUnreadCount%2522%253A%25220%2522%252C%2522onum%2522%253A%25220%2522%252C%2522claimPoint%2522%253A%25220%2522%252C%2522token%2522%253A%2522eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxMzg0MTIxMjk2NiIsImlhdCI6MTU3NjMyNDg1NSwiZXhwIjoxNjA3ODYwODU1fQ.JxfGEGs3edD1k74hiCh3WAuCxK_UKr1rdTrkwSx7L5Y9VqmdEi8nOOLVsJBcbW5H3a53QihmH7d17D3pPphB1A%2522%252C%2522pleaseAnswerCount%2522%253A%25220%2522%252C%2522redPoint%2522%253A%25220%2522%252C%2522bizCardUnread%2522%253A%25220%2522%252C%2522vnum%2522%253A%25220%2522%252C%2522mobile%2522%253A%252213841212966%2522%257D; auth_token=eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxMzg0MTIxMjk2NiIsImlhdCI6MTU3NjMyNDg1NSwiZXhwIjoxNjA3ODYwODU1fQ.JxfGEGs3edD1k74hiCh3WAuCxK_UKr1rdTrkwSx7L5Y9VqmdEi8nOOLVsJBcbW5H3a53QihmH7d17D3pPphB1A; tyc-user-phone=%255B%252213841212966%2522%255D; Hm_lpvt_e92c8d65d92d534b0fc290df538b4758=1576324867",
            "DNT": "1",
            "Host": "www.tianyancha.com",
            "Sec-Fetch-Mode": "navigate",
            "Sec-Fetch-Site": "none",
            "Sec-Fetch-User": "?1",
            "Upgrade-Insecure-Requests": "1",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"
        }


    def get_contents(self):
        postData = {
            "key": "杭州泽瑞科环保科技有限公司"
        }
        pageNo = 6
        url = f"https://www.tianyancha.com/search/p{pageNo}"
        html = HttpUtils.do_request("GET", url, self.headers, postData)
        root = etree.HTML(html.text)
        selector = root.xpath('//div[@class="search-item sv-search-company"][1]/div/div[@class="content"]')

        company = "".join(selector[0].xpath('./div[@class="header"]/a//text()'))
        tag = "|".join(selector[0].xpath('./div[@class="tag-list"]/div//text()'))
        fr = selector[0].xpath('./div[@class="info row text-ellipsis"]/div[1]/a/@title')[0]
        ziben = selector[0].xpath('./div[@class="info row text-ellipsis"]/div[2]/a/@title')[0]
        clrq = selector[0].xpath('./div[@class="info row text-ellipsis"]/div[3]/span/text()')[0]
        tel = selector[0].xpath('./div[@class="contact row "]/div[1]/script/text()')[0]
        email = selector[0].xpath('./div[@class="contact row "]/div[2]/script/text()')[0]
        # for x in selector:
        #     company = "".join(x.xpath('./div[@class="header"]/a//text()'))
        #     tag = "|".join(x.xpath('./div[@class="tag-list"]/div//text()'))
        #     fr = x.xpath('./div[@class="info row text-ellipsis"]/div[1]/a/@title')[0]
        #     ziben = x.xpath('./div[@class="info row text-ellipsis"]/div[2]/a/@title')[0]
        #     clrq = x.xpath('./div[@class="info row text-ellipsis"]/div[3]/span/text()')[0]
        #     tel = x.xpath('./div[@class="contact row "]/div[1]/script/text()')[0]
        #     email = x.xpath('./div[@class="contact row "]/div[2]/script/text()')[0]


if __name__ == '__main__':
    tianyancha = TianYanCha()
    tianyancha.get_contents()